package dao.impl;

import dao.BaseDAO;
import dao.UserDAO;
import pojo.UserDO;

import java.sql.SQLException;

public class UserDAOImpl extends BaseDAO implements UserDAO {

    @Override
    public UserDO getByUsernameAndPwd(String username, String pwd) {
        con = this.getConnection();
        statement = null;
        rs = null;
        UserDO userDO = null;
        String sql = "select * from t_user where u_name =? and u_password = ?   ";
        try {
            statement = con.prepareStatement(sql);
            statement.setObject(1, username);
            statement.setObject(2, pwd);
            rs = statement.executeQuery();
            if (rs.next()) {
                userDO = new UserDO();
                userDO.setName(rs.getString("u_name"));
                userDO.setId(rs.getInt("u_id"));
                userDO.setPassword(rs.getString("u_password"));
                userDO.setBalance(rs.getInt("u_balance"));
            }
        } catch (SQLException exception) {
            exception.printStackTrace();
        } finally {
            close(con, statement, rs);
        }
        return userDO;
    }

    @Override
    public UserDO getById(int id) {
        getConnection();
        statement = null;
        rs = null;
        UserDO userDO = null;
        String sql = "select * from t_user where u_id =?";
        try {
            statement = con.prepareStatement(sql);
            statement.setObject(1, id);
            rs = statement.executeQuery();

            if (rs.next()) {
                userDO = new UserDO();
                userDO.setId(rs.getInt("u_id"));
                userDO.setName(rs.getString("u_name"));
                userDO.setPassword(rs.getString("u_password"));
                userDO.setBalance(rs.getInt("u_balance"));
            }
        } catch (SQLException exception) {
            exception.printStackTrace();
        } finally {
            close(con, statement, rs);
        }
        return userDO;
    }

    @Override
    public UserDO getByName(String name) {
        getConnection();
        statement = null;
        rs = null;
        UserDO userDO = null;
        String sql = "select * from t_user where u_name =?";
        try {
            statement = con.prepareStatement(sql);
            statement.setObject(1, name);
            rs = statement.executeQuery();

            if (rs.next()) {
                userDO = new UserDO();
                userDO.setId(rs.getInt("u_id"));
                userDO.setName(rs.getString("u_name"));
                userDO.setPassword(rs.getString("u_password"));
                userDO.setBalance(rs.getInt("u_balance"));
            }
        } catch (SQLException exception) {
            exception.printStackTrace();
        } finally {
            close(con, statement, rs);
        }
        return userDO;
    }

    @Override

    public void update(UserDO userDO) {
        statement = null;
        String sql = "UPDATE t_user set u_balance = ? , u_password=?  where u_id = ? ";
        try {
            statement = con.prepareStatement(sql);
            statement.setObject(1, userDO.getBalance());
            statement.setObject(2, userDO.getPassword());
            statement.setObject(3, userDO.getId());
            int row = statement.executeUpdate();
            System.out.println(row);
        } catch (SQLException exception) {
            exception.printStackTrace();
        }
    }


    //更新用户
//    public  void update (UserDO userDO, UserDO userDO2) {
//        con = getConnection();
//        statement = null;
//        String sql = "UPDATE t_user set u_balance = ? , u_password=?  where u_id = ? " ;
//        try {
//            con.setAutoCommit(false);
//            statement = con.prepareStatement(sql);
//            statement.setObject(1, userDO.getBalance());
//            statement.setObject(2, userDO.getPassword());
//            statement.setObject(3, userDO.getId());
//            int row = statement.executeUpdate();
//            System.out.println(row);
//            statement = con.prepareStatement(sql);
//            statement.setObject(1, userDO2.getBalance());
//            statement.setObject(2, userDO2.getPassword());
//            statement.setObject(3, userDO2.getId());
//            row = statement.executeUpdate();
//            con.commit();
//            System.out.println(row);
//        } catch (SQLException exception) {
//            exception.printStackTrace();
//            try {
//                con.rollback();
//            } catch (SQLException throwables) {
//                throwables.printStackTrace();
//            }
//        } finally {
//            close(con, statement);
//        }
//    }

}
